package model

import (
	"database/sql/driver"
	//"encoding/json"
	"encoding/json"
)

var HostGroups HostGroup

// 服务器组
type HostGroup struct {
	//gorm.Model
	ID          int64        `gorm:"primaryKey" json:"id"`
	GroupName   string       `json:"group_name"`
	Note        string       `json:"note"`
	HostServers []HostServer `gorm:"many2many:host_group_host_server;"`
}

// 主机服务器数据结构
type HostServer struct {
	ID               int64        `gorm:"primaryKey" json:"id"`
	Hostname         string       `json:"hostname"`
	PublicIp         string       `json:"public_ip" `                          //公网IP
	PrivateIP        PrivateIPs   `json:"private_ip" gorm:"type:varchar(100)"` //内网IP
	OsVersion        string       `json:"os_version"`                          //系统版本
	Disk             Disks        `json:"disk" gorm:"type:varchar(100)"`
	MachineType      string       `json:"machine_type"` //服务器类型
	Cpu              string       `json:"cpu_num"`
	Memory           string       `json:"memory"`
	Put_shelves_date string       `json:"put_shelves_date"`                  //创建时间
	Ssh_ip           string       `json:"ssh_ip"`                            //连接IP地址
	Ssh_port         string       `json:"ssh_port"`                          //连接端口
	HostGroups       []HostGroup  `gorm:"many2many:host_group_host_server;"` //多对多服务器组
	Credentials      []Credential `gorm:"many2many:credential_hostserver;"`  //多对多服务器组凭据
	//Credentials      []Credential `gorm:"foreignKey:HostServerID"`           //一对多凭据
	//CredentialID uint
}

// 服务器登入凭据
type Credential struct {
	ID          int64        `gorm:"primaryKey" json:"id"`
	Name        string       `json:"name"`
	User        string       `json:"user"`
	Password    string       `json:"password"`
	HostServers []HostServer `gorm:"many2many:credential_hostserver;"`
}

// PrivateIP字段自定义
type PrivateIP struct {
	IP     string `json:"ip"`
	IPName string `json:"ip_name"`
}

type PrivateIPs []PrivateIP

func (t *PrivateIPs) Scan(value interface{}) error {
	bytesValue, _ := value.([]byte)
	return json.Unmarshal(bytesValue, t)
}

func (t PrivateIPs) Value() (driver.Value, error) {
	return json.Marshal(t)
}

// Disk字段自定义
type Disk struct {
	Device string `json:"device"`
	Type   string `json:"type"`
	Size   string `json:"size"`
}

type Disks []Disk

func (t *Disks) Scan(value interface{}) error {
	bytesValue, _ := value.([]byte)
	return json.Unmarshal(bytesValue, t)
}

func (t Disks) Value() (driver.Value, error) {
	return json.Marshal(t)
}
